from .database import DatabaseManager

def check_crawl_results():
    db = DatabaseManager()
    db.connect()
    
    try:
        # 查看已完成的资源
        with db.connection.cursor() as cursor:
            cursor.execute('SELECT id, share_code, share_status, file_info_json FROM cloud_resources WHERE share_status = "processed" ORDER BY updated_at DESC LIMIT 5')
            completed_resources = cursor.fetchall()
        
        print('已完成的资源:')
        for r in completed_resources:
            file_info_length = len(r[3]) if r[3] else 0
            print(f'ID: {r[0]}, 分享码: {r[1]}, 状态: {r[2]}, 文件信息长度: {file_info_length}')
            
            # 如果有文件信息，显示部分内容
            if r[3]:
                import json
                try:
                    data = json.loads(r[3])
                    print(f'  - 成功状态: {data.get("success", "未知")}')
                    print(f'  - 文件数量: {len(data.get("file_info", []))}')
                    print(f'  - 统计信息: {data.get("statistics", {})}')
                except:
                    print(f'  - JSON解析失败')
            print()
        
        # 查看待处理的资源数量
        with db.connection.cursor() as cursor:
            cursor.execute('SELECT COUNT(*) FROM cloud_resources WHERE share_status = "unprocessed"')
            pending_count = cursor.fetchone()[0]
        print(f'待处理资源数量: {pending_count}')
        
        # 查看所有状态的统计
        with db.connection.cursor() as cursor:
            cursor.execute('SELECT share_status, COUNT(*) FROM cloud_resources GROUP BY share_status')
            status_stats = cursor.fetchall()
        print('\n状态统计:')
        for status, count in status_stats:
            print(f'  {status}: {count}')
            
    except Exception as e:
        print(f'查询失败: {e}')
    
    db.disconnect()

if __name__ == '__main__':
    check_crawl_results()